/**
 * 变量定义
 */
package com.swp.oa.comomn;


/**
 * @author lvly-design
 * @author hanl-implement
 * 
 */
public class Variable {
	
	public enum IOType{
		input, output, any,
	}

	// 变量类型
	public enum VarType {
		VT_BOOLEAN,
		VT_INTEGER,
		VT_DOUBLE,
		VT_STRING,
		
		// 向量
		VT_BOOLEAN_VECTOR,
		VT_INTEGER_VECTOR,
		VT_DOUBLE_VECTOR,
		VT_STRING_VECTOR,
				
		// 多维数组
		VT_BOOLEAN_ARRAY,
		VT_INTEGER_ARRAY,
		VT_DOUBLE_ARRAY,
		VT_STRING_ARRAY,	
	}
	
	// IO标记
	public final static String INPUT_FLAG = "input";
	public final static String OUTPUT_FLAG = "output";

	// 常量定义
	public final static String BOOLEAN = "bool";
	public final static String INTEGER = "int";
	public final static String DOUBLE = "double";
	public final static String STRING = "String";
	
	// 向量
	public final static String BOOLEAN_VECTOR = "vector<bool>";
	public final static String INTEGER_VECTOR = "vector<int>";
	public final static String DOUBLE_VECTOR = "vector<double>";
	public final static String STRING_VECTOR = "vecotr<String>";
	
	// 数组定义
	public final static String BOOLEAN_ARRAY = "bool[]";
	public final static String INTEGER_ARRAY = "int[]";
	public final static String DOUBLE_ARRAY = "double[]";
	public final static String STRING_ARRAY = "String[]";

	public String name;
	public String type;
	public String value;
	private String ioFlag = INPUT_FLAG;
	
	/**
	 * 构造函数
	 */
	public Variable(){
		this.ioFlag = INPUT_FLAG;
	}
	
	/**
	 * 构造函数
	 * @param isInput 该变量是否为输入变量
	 */
	public Variable(boolean isInput){
		if (isInput) {  //判断是否为输入变量
			this.ioFlag = INPUT_FLAG;
		}
		else{
			this.ioFlag = OUTPUT_FLAG;
		}
	}
	
	/**
	 * 获取改变量是否为输入变量
	 * @return ture 为是否变量 false 为输出变量
	 */
	public boolean isInput()
	{
		return this.ioFlag.equalsIgnoreCase(INPUT_FLAG);
	}
	
	/**
	 * 设置该变量的标记
	 * @param isInput 是否为输入变量
	 */
	public void setIOFlag(boolean isInput){
		if (isInput) { //判断是否为输入变量
			this.ioFlag = INPUT_FLAG;
		}
		else{ 
			//设置输入输出标记为输出变量
			this.ioFlag = OUTPUT_FLAG;
		}
	}
	
	/**
	 * 获取变量类型
	 * @return 变量类型
	 */
	public VarType getVarType(){
		return Variable.toVarType(this.type);
	}
	
	/**
	 * 当前变量为bool类型，返回该bool类型值
	 * @return 变量值
	 */
	public boolean getValueBool() {
		assert (type.equalsIgnoreCase(BOOLEAN));
		boolean b = Boolean.valueOf(value).booleanValue();
		return b;
	}

	/**
	 * 当前变量为int类型，返回该int类型值
	 * @return 变量值
	 */
	public int getValueInteger() {
		assert (type.equalsIgnoreCase(INTEGER));
		int c = (int)Double.parseDouble(value);
		return c;
	}
	
	/**
	 * 当前变量为double类型，返回该double类型值
	 * @return 变量值
	 */
	public double getValueDouble() {
		assert (type.equalsIgnoreCase(DOUBLE));
		double d = Double.parseDouble(value);
		return d;
	}
	
	/**
	 * 当前变量为string类型，返回该string类型值
	 * @return 变量值
	 */
	public String getValueString(){
		return value;
	}
	
	/**
	 * 当前类型为boolean[]类型，返回其值
	 * @return 变量值
	 */
	public boolean []getValueBoolArray(){
		assert (type.equalsIgnoreCase(BOOLEAN_ARRAY));
		boolean [] b = stringToBoolArray(value);
		return b;
	}
	
	/**
	 * 当前类型为int[]类型，返回其值
	 * @return 变量值
	 */
	public int[] getValueIntegerArray() {
		assert (type.equalsIgnoreCase(INTEGER_ARRAY));
		int[] c = stringToIntArray(value);
		return c;
	}

	/**
	 * 当前类型为double[]类型，返回其值
	 * @return 变量值
	 */
	public double[] getValueDoubleArray() {
		assert (type.equalsIgnoreCase(DOUBLE_ARRAY));
		double[] d = stringToDoubleArray(value);
		return d;
	}

	/**
	 * 将当前变量以“，”分割，返回字符串数组类型
	 * @return 变量值
	 */
	public String[] getValueStringArray(){
		return value.split(",");
	}
	/**
	 * 类型字符串转换成枚举类型
	 * 
	 * @param typeStr
	 *            类型字符串
	 * @return 枚举类型
	 */
	public static VarType toVarType(String type) {
		VarType var = null;

		// boolean
		if (type.equalsIgnoreCase(BOOLEAN)) {
			var = VarType.VT_BOOLEAN;

			// integre
		} else if (type.equalsIgnoreCase(INTEGER)) {
			var = VarType.VT_INTEGER;

			// double
		} else if (type.equalsIgnoreCase(DOUBLE)) {
			var = VarType.VT_DOUBLE;
			
			// string
		}else if(type.equalsIgnoreCase(STRING)){
			var = VarType.VT_STRING;
			
			// boolean[]
		}else if (type.equalsIgnoreCase(BOOLEAN_ARRAY) || type.equalsIgnoreCase("boolarray")){
			var = VarType.VT_BOOLEAN_ARRAY;
			
			// double[]
		} else if (type.equalsIgnoreCase(DOUBLE_ARRAY) || type.equalsIgnoreCase("doublearray")){
			var = VarType.VT_DOUBLE_ARRAY;
			
			// int[]
		} else if (type.equalsIgnoreCase(INTEGER_ARRAY)|| type.equalsIgnoreCase("intarray")){
			var = VarType.VT_INTEGER_ARRAY;
			
			// string[]
		} else if (type.equalsIgnoreCase(STRING_ARRAY) || type.equalsIgnoreCase("stringarray")){
			var = VarType.VT_STRING_ARRAY;
		}else {
			
		}

		return var;
	}

	/**
	 * 枚举类型转换成类型字符串
	 * 
	 * @param vt
	 *            枚举类型
	 * @return 类型字符串
	 */
	public static String toVarType(VarType type) {
		String s = DOUBLE; // 缺省
		
		switch (type) {	
		case VT_BOOLEAN: //boolean
			s = BOOLEAN;
			break;
		case VT_INTEGER:  // int
			s = INTEGER;
			break;
		case VT_DOUBLE:  //double
			s = DOUBLE;
			break;
		case VT_STRING:  //string
			s = STRING;
			break;
		case VT_BOOLEAN_ARRAY: //boolean[]
			s = BOOLEAN_ARRAY;
			break;
		case VT_INTEGER_ARRAY:  //int[]
			s = INTEGER_ARRAY;
			break;
		case VT_DOUBLE_ARRAY:  //double[]
			s = DOUBLE_ARRAY;
			break;
		case VT_STRING_ARRAY:  //string[]
			s = STRING_ARRAY;
			break;
		default:
			s = DOUBLE;
			break;
		}

		return s;
	}
	
	/**
	 * 将string类型的字符串转换成double数组
	 * @param inputString 输入字符串
	 * @return double数组
	 */
	public static double [] stringToDoubleArray(String inputString){
		String [] array = null;
		double [] doubleArray = null;
		
		//将字符串用，分割开
		array=inputString.split(","); 
		
		// 声明新的数组
		doubleArray = new double[array.length];  
		
		int index = 0;
		for (String s : array) {
			
			//将字符串转换成double类型 
			double d = Double.parseDouble(s);
			doubleArray[index++] = d ; 
		}
		return doubleArray;
	}
	
	/**
	 * 将string类型的字符串转换成int数组
	 * @param inputString
	 * @return
	 */
	public static int [] stringToIntArray(String inputString){
		String [] array = null;
		int [] intArray = null;
		
		//将字符串用，分割开
		array=inputString.split(",");
		
		// 声明新的数组
		intArray = new int[array.length];
		
		int index = 0;
		for (String s : array) {
			//将字符串转换成int类型 
			int d = Integer.parseInt(s);
			intArray[index++] = d ; 
		}
		return intArray;
	}
	
	/**
	 * 将string类型的字符串转换成bool数组
	 * @param inputString
	 * @return
	 */
	public static boolean [] stringToBoolArray(String inputString){
		String [] array = null;
		boolean [] boolArray = null;
		
		//将字符串用，分割开
		array=inputString.split(",");
		
		// 声明新的数组
		boolArray = new boolean[array.length];
		
		int index = 0;
		for (String s : array) {
			//将字符串转换成boolean类型 
			boolean d = Boolean.parseBoolean(s);
			boolArray[index++] = d ; 
		}
		return boolArray;
	}
	
	/**
	 * 将boolean[]转换成string类型
	 * @param input 输入的boolean[]
	 * @return 转换后的字符串
	 */
	public static String boolArrToString(boolean [] input){
		String s = "";

		if(input.length>0){
			
			s+= input[0];
			
			for(int i=1; i < input.length;i++){
				s+=",";
				s+=input[i];
			}
		}
		return s;
	}
	
	/**
	 * 将int[]转换成string类型
	 * @param input 输入的int[]
	 * @return 转换后的字符串
	 */
	public static String intArrToString(int [] input){
		String s = "";

		if(input.length>0){
			
			s+= input[0];
			
			for(int i=1; i < input.length;i++){
				s+=",";
				s+=input[i];
			}
		}
		return s;
	}
	
	/**
	 * 将boolean[]转换成int[]类型
	 * @param input 输入的boolean[]
	 * @return 转换后的int[]
	 */
	public static String doubleArrToString(double [] input){
		String s = "";

		if(input.length>0){
			
			s+= input[0];
			
			for(int i=1; i < input.length;i++){
				s+=",";
				s+=input[i];
			}
		}
		return s;
	}
} // end class

